local skel = fk.CreateSkill {
  name = "emo__yuyi",
}

Fk:loadTranslationTable{
  ["emo__yuyi"] = "隅意",
  [":emo__yuyi"] = "每回合限两次，每当你距离1以内的角色受到伤害后，你可以选择一项：1，令其摸X张牌，然后弃置Y张牌；2，你摸X张牌，然后将Y张牌以任意顺序置于牌堆顶；3，你失去一点体力发动〖修身〗（X为其体力上限，Y为你计算距离大于1的角色数）。",

  ["#emo__yuyi-invoke"] = "隅意：可以对 %src 发动“隅意”",
  ["emo__yuyi1"] = "令 %dest 摸 %arg 张牌并弃置 %arg2 张牌",
  ["emo__yuyi2"] = "你摸 %arg 张牌，并将 %arg2 张牌置于牌堆顶",
  ["emo__yuyi3"] = "你失去一点体力发动〖修身〗",
  ["#emo__yuyi-put"] = "将 %arg 张牌置于牌堆顶",

  ["$emo__yuyi1"] = "隅角草木为盛，皆我泪沃之。",
  ["$emo__yuyi2"] = "胭脂败彩，以泪洗面。",
}

skel:addEffect(fk.Damaged, {
  anim_type = "masochism",
  times = function (_, player)
    return 2 - player:usedSkillTimes(skel.name, Player.HistoryTurn)
  end,
  can_trigger = function(self, event, target, player, data)
    return (player:distanceTo(target) == 1 or target == player) and not target.dead
    and player:hasSkill(skel.name) and player:usedSkillTimes(skel.name, Player.HistoryTurn) < 2
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#emo__yuyi-invoke:"..target.id})
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local x = target.maxHp
    local y = #table.filter(room.alive_players, function(p) return player:distanceTo(p) > 1 end)
    local all_choices = {"emo__yuyi1::"..target.id..":"..x..":"..y, "emo__yuyi2::"..":"..x..":"..y, "emo__yuyi3"}
    local choices = table.simpleClone(all_choices)
    if target.dead then table.remove(choices, 1) end
    local choice = room:askToChoice(player, { choices = choices, skill_name = skel.name, all_choices = all_choices})
    if choice:startsWith("emo__yuyi1") then
      target:drawCards(x, skel.name)
      if not target.dead and y > 0 then
        room:askToDiscard(player, {min_num = y, max_num = y, include_equip = true, skill_name = skel.name, cancelable = false})
      end
    elseif choice:startsWith("emo__yuyi2") then
      player:drawCards(x, skel.name)
      if not player.dead and y > 0 and not player:isNude() then
        local put = (#player:getCardIds("he") <= y) and player:getCardIds("he") or
        room:askToCards(player, {
          min_num = y, max_num = y, include_equip = true, skill_name = skel.name, cancelable = false,
          prompt = "#emo__yuyi-put:::"..y
        })
        if #put > 1 then
          put = room:askToGuanxing(player, { cards = put, bottom_limit = {0,0}, skill_name = skel.name, skip = true}).top
        end
        room:moveCards({
          ids = table.reverse(put),
          from = player,
          toArea = Card.DrawPile,
          moveReason = fk.ReasonPut,
          skillName = skel.name,
          proposer = player,
        })
      end
    else
      room:loseHp(player, 1, skel.name)
      if player.dead then return end
      local skill = Fk.skills["emo__xiushen"]---@type TriggerSkill
      if skill and skill:isInstanceOf(TriggerSkill) then
        room:useSkill(player, skill, function()
          return skill:use(event, player, player, data)
        end)
      end
    end
  end,
})

return skel
